﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PlateNumber {
    public partial class TestRangeForm : Form {
        private int[] _numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        private string[] _letters4 = { "C", "D", "E", "F" };
        private string[] _letters5 = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y" };

        public List<string> Numbers { get; private set; }

        public TestRangeForm() {
            InitializeComponent();
        }

        private void TestRangeForm_Load(object sender, EventArgs e) {
            ddlFrom4.SelectedIndex = 0;
            ddlFrom5.SelectedIndex = 0;
            ddlTo4.SelectedIndex = 0;
            ddlTo5.SelectedIndex = 0;
        }

        private void Priview() {
            int from1 = (int)numFrom1.Value;
            int from2 = (int)numFrom2.Value;
            int from3 = (int)numFrom3.Value;
            string from4 = ddlFrom4.Text;
            string from5 = ddlFrom5.Text;

            int to1 = (int)numTo1.Value;
            int to2 = (int)numTo2.Value;
            int to3 = (int)numTo3.Value;
            string to4 = ddlTo4.Text;
            string to5 = ddlTo5.Text;

            List<string> numbers = GetNumbers(from1, from2, from3, from4, from5,
                to1, to2, to3, to4, to5, 4);

            lblExample.Text = String.Empty;
            for (int i = 0; i < (numbers.Count == 4 ? numbers.Count - 1 : numbers.Count); ++i) {
                lblExample.Text += numbers[i];
                if (i < (numbers.Count == 4 ? numbers.Count - 2 : numbers.Count - 1)) {
                    lblExample.Text += ", ";
                }
            }

            string to = String.Format("{0}{1}{2}{3}{4}", to1, to2, to3, to4, to5);

            if (numbers.Count == 4) {
                if (numbers[numbers.Count - 1] != to) {
                    lblExample.Text += " ... " + to;
                }
                else {
                    lblExample.Text += ", " + to;
                }
            }
        }

        private List<string> GetNumbers(
            int from1, int from2, int from3, string from4, string from5, 
            int to1, int to2, int to3, string to4, string to5, 
            int count) {

            List<string> numbers = new List<string>();

            for (int p1 = from1; p1 <= to1; ++p1) {
                for (int p2 = from2; p2 <= to2; ++p2) {
                    for (int p3 = from3; p3 <= to3; ++p3) {
                        for (int p4 = GetPosition(_letters4, from4); p4 <= GetPosition(_letters4, to4); ++p4) {
                            for (int p5 = GetPosition(_letters5, from5); p5 <= GetPosition(_letters5, to5); ++p5) {
                                numbers.Add(String.Format("{0}{1}{2}{3}{4}", p1, p2, p3, _letters4[p4], _letters5[p5]));
                                if (count != -1) {
                                    --count;
                                    if (count <= 0) {
                                        return numbers;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return numbers;
        }

        private int GetPosition(string[] arr, string value) {
            for (int i = 0; i < arr.Length; ++i) {
                if (arr[i] == value) {
                    return i;
                }
            }
            return -1;
        }

        private void btnOK_Click(object sender, EventArgs e) {
            int from1 = (int)numFrom1.Value;
            int from2 = (int)numFrom2.Value;
            int from3 = (int)numFrom3.Value;
            string from4 = ddlFrom4.Text;
            string from5 = ddlFrom5.Text;

            int to1 = (int)numTo1.Value;
            int to2 = (int)numTo2.Value;
            int to3 = (int)numTo3.Value;
            string to4 = ddlTo4.Text;
            string to5 = ddlTo5.Text;

            Numbers = GetNumbers(from1, from2, from3, from4, from5,
                to1, to2, to3, to4, to5, -1);

            if (MessageBox.Show(this, "系统将自动生成 " + Numbers.Count + " 条探测数据，您确定吗？", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) {
                return;
            }

            DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            Close();
            DialogResult = DialogResult.Cancel;
        }

        private void btnPreview_Click(object sender, EventArgs e) {
            Priview();
        }
    }
}
